package com.tiny.work.db;

import java.sql.Connection;
import java.sql.DriverManager;

public class DBUtil {
    private static final String driver = "com.mysql.jdbc.Driver";
    private static final String url = "jdbc:mysql://localhost:3306/Test1";
    private static final String username = "root";
    private static final String password = "admin";

    // 获取连接
    public static Connection getConnection() {
        try {
            Class.forName(driver);
            Connection conn = DriverManager.getConnection(url, username, password);
            System.out.printf("%s,创建一个链接:%s\n",Thread.currentThread().getName(), conn.toString());
            return conn;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    // 关闭连接
    public static void closeConnection(Connection conn) {
        try {
            if (conn != null) {
                conn.close();
                System.out.printf("%s,关闭一个链接\n",Thread.currentThread().getName());

            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


}